package com.woniuxy.carlife.carlifeuserweb.controller;


import cn.hutool.core.util.ObjectUtil;
import com.woniuxy.carlife.carlifecommonsweb.exception.CarlifeGlobalException;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.carlifecommonsweb.util.CopyUtil;
import com.woniuxy.carlife.carlifecommonsweb.util.JwtUtil;
import com.woniuxy.carlife.carlifeusermodel.dto.*;
import com.woniuxy.carlife.carlifeusermodel.param.ShippingaddressParam;
import com.woniuxy.carlife.carlifeuserweb.entity.*;
import com.woniuxy.carlife.carlifeuserweb.service.CityService;
import com.woniuxy.carlife.carlifeuserweb.service.ProvinceService;
import com.woniuxy.carlife.carlifeuserweb.service.RegionService;
import com.woniuxy.carlife.carlifeuserweb.service.ShippingaddressService;
import com.woniuxy.carlife.carlifeuserweb.util.StatusMessageEnum;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.*;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zjq
 * @ClassName:ShippingaddressController
 * @Description:收货地址
 * @date 2021/4/6 20:02
 **/

@RestController
@RequestMapping("/shippingaddress")
@Api(tags = "用户收货地址接口信息")//用于描述接口类的相关信息，作用于类上
public class ShippingaddressController {

    @Resource
    ShippingaddressService shippingaddressService;

    //省
    @Resource
    ProvinceService provinceService;

    //    市
    @Resource
    CityService cityService;

    //区
    @Resource
    RegionService regionService;

    /*
     * 功能描述〈根据用户查询所有的收货地址〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 16:08
     * @throws
     */

    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "根据用户查询所有的收货地址,解析token")
    //@ApiResponses用于描述响应状态信息

    @GetMapping("/ordinary/findShippingaddressByUserId")
    public Result findShippingaddressByUserId() {
        ServletRequestAttributes sra =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        if (claims == null) {

            return new Result(false, StatusCode.ERROR, StatusMessageEnum.NOT_LOGGED_IN.toString());
        }
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));
        List<Shippingaddress> shippingaddressByUserId = shippingaddressService.findShippingaddressByUserId(tokenUserId);

        if (ObjectUtil.isNull(shippingaddressByUserId)) {
            return new Result(false, StatusCode.ERROR, StatusMessageEnum.QUERY_IS_EMPTY.toString());
        }

        List<ShippingaddressDto> shippingaddressList = CopyUtil.getList(shippingaddressByUserId, ShippingaddressDto.class);
        if (ObjectUtil.isNull(shippingaddressList)) {
            return new Result(false, StatusCode.QUERY_IS_EMPTY, StatusMessageEnum.QUERY_IS_EMPTY.toString(), shippingaddressList);

        }
        return new Result(true, StatusCode.SEARCH_SUCCESSFUL, StatusMessageEnum.SEARCH_SUCCESSFUL.toString(), shippingaddressList);
    }


    /*
     * 功能描述〈根据用户id查询默认收货地址〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 16:01
     * @throws
     */

    //@ApiOperation用于描述接口方法，作用于方法上

    @ApiOperation(value = "根据用户id查询默认收货地址,解析token")
    //@ApiResponses用于描述响应状态信息

    @GetMapping("/ordinary/findShippingaddressDefaultByUserId")
    public Result findShippingaddressDefaultByUserId() {
        ServletRequestAttributes sra =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        if (claims == null) {

            return new Result(false, StatusCode.NOT_LOGGED_IN, StatusMessageEnum.NOT_LOGGED_IN.toString());
        }
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));
        Shippingaddress shippingaddressDefaultByUserId = shippingaddressService.findShippingaddressDefaultByUserId(tokenUserId);

        if (ObjectUtil.isNull(shippingaddressDefaultByUserId)) {
            return new Result(false, StatusCode.QUERY_IS_EMPTY, StatusMessageEnum.QUERY_IS_EMPTY.toString());
        }

        ShippingaddressDto findShippingaddressDefault = CopyUtil.getOne(shippingaddressDefaultByUserId, ShippingaddressDto.class);

        return new Result(true, StatusCode.SEARCH_SUCCESSFUL, StatusMessageEnum.SEARCH_SUCCESSFUL.toString(), findShippingaddressDefault);
    }

    /*
     * 功能描述〈修改地址〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/13 5:35
     * @throws
     */
    @ApiOperation(value = "修改地址")
    //@ApiResponses用于描述响应状态信息

    @PostMapping("/ordinary/modifyShippingaddressByshippingaddressId")
    public Result modifyShippingaddressByshippingaddressId(@ApiParam ShippingaddressDto shippingaddressDto) {
        if (ObjectUtil.isNull(shippingaddressDto)) {
            return new Result(false, StatusCode.CAN_NOT_BE_EMPTY, StatusMessageEnum.CAN_NOT_BE_EMPTY.toString());
        }
        ServletRequestAttributes sra =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        if (claims == null) {

            return new Result(false, StatusCode.NOT_LOGGED_IN, StatusMessageEnum.NOT_LOGGED_IN.toString());
        }
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));
        boolean b = shippingaddressService.modifyShippingaddressByshippingaddressId(shippingaddressDto);
        return b ?
                new Result(true, StatusCode.SUCCESSFULLY_MODIFIED, StatusMessageEnum.SUCCESSFULLY_MODIFIED.toString())
                : new Result(false, StatusCode.FAIL_TO_EDIT, StatusMessageEnum.FAIL_TO_EDIT.toString());


    }

    /*
     * 功能描述〈删除地址〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/13 8:53
     * @throws
     */
    @ApiOperation(value = "删除地址")
    //@ApiResponses用于描述响应状态信息

    @PostMapping("/ordinary/deleteShippingaddressByshippingaddressId")
    public Result deleteShippingaddressByshippingaddressId(Integer shippingaddressId) {

        if (ObjectUtil.isNull(shippingaddressId)) {
            return new Result(false, StatusCode.CAN_NOT_BE_EMPTY, StatusMessageEnum.CAN_NOT_BE_EMPTY.toString());
        }
        ServletRequestAttributes sra =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        if (claims == null) {

            return new Result(false, StatusCode.NOT_LOGGED_IN, StatusMessageEnum.NOT_LOGGED_IN.toString());
        }
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));
        if (ObjectUtil.isNull(shippingaddressId)) {
            return new Result(true, StatusCode.CAN_NOT_BE_EMPTY, StatusMessageEnum.CAN_NOT_BE_EMPTY.toString());

        }
        boolean b = shippingaddressService.deleteShippingaddressByshippingaddressId(shippingaddressId);
        return b ?
                new Result(true, StatusCode.SUCCESSFULLY_DELETED, StatusMessageEnum.SUCCESSFULLY_DELETED.toString())
                : new Result(false, StatusCode.FAILED_TO_DELETE, StatusMessageEnum.FAILED_TO_DELETE.toString());


    }

    /*
     * 功能描述〈新增地址〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/14 20:54
     * @throws
     */
    @ApiOperation(value = "新增地址")
    //@ApiResponses用于描述响应状态信息

    @PostMapping("/ordinary/addShippingaddress")
    public Result addShippingaddress(ShippingaddressParam shippingaddressParam) {
        if (ObjectUtil.isNull(shippingaddressParam)) {
            return new Result(false, StatusCode.CAN_NOT_BE_EMPTY, StatusMessageEnum.CAN_NOT_BE_EMPTY.toString());

        }
        ServletRequestAttributes sra =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        if (claims == null) {

            return new Result(false, StatusCode.NOT_LOGGED_IN, StatusMessageEnum.NOT_LOGGED_IN.toString());
        }
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));
        Shippingaddress one = CopyUtil.getOne(shippingaddressParam, Shippingaddress.class);
        one.setUserId(tokenUserId);
        Boolean aBoolean = shippingaddressService.addShippingaddress(one);
        return aBoolean ?
                new Result(true, StatusCode.ADDED_SUCCESSFULLY, StatusMessageEnum.ADDED_SUCCESSFULLY.toString())
                : new Result(false, StatusCode.ADD_FAILED, StatusMessageEnum.ADD_FAILED.toString());


    }

    /*
     * 功能描述〈查询省〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 15:10
     * @throws
     */
    @ApiOperation(value = "查询省")

    @GetMapping("/ordinary/findProvince")
    public Result findProvince() {
        List<Province> province = provinceService.findProvince();
        List<ProvinceDto> provinceList = CopyUtil.getList(province, ProvinceDto.class);
        return province == null ?
                new Result(false, StatusCode.ERROR, StatusMessageEnum.QUERY_IS_EMPTY.toString())
                : new Result(true, StatusCode.OK, StatusMessageEnum.SEARCH_SUCCESSFUL.toString(), provinceList);
    }


    /*
     * 功能描述〈根据省id查询市〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 15:20
     * @throws
     */

    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "查询市，根据省id")

    @ApiImplicitParams({
            @ApiImplicitParam(name = "provinceId", value = "省id"
                    , example = "3", required = true, paramType = "query", dataType = "int")
    })
    @PostMapping("/ordinary/findCityByProvinceId")
    public Result findCityByProvinceId(Integer provinceId) {
        List<City> cityByProvinceId = cityService.findCityByProvinceId(provinceId);
        List<CityDto> cityList = CopyUtil.getList(cityByProvinceId, CityDto.class);
        return cityList == null ?
                new Result(false, StatusCode.ERROR, StatusMessageEnum.QUERY_IS_EMPTY.toString())
                : new Result(true, StatusCode.OK, StatusMessageEnum.SEARCH_SUCCESSFUL.toString(), cityList);
    }

    /*
     * 功能描述〈根据市id查询地区〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 15:36
     * @throws
     */
    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "查询区，根据市id")

    @ApiImplicitParams({
            @ApiImplicitParam(name = "cityId", value = "市id"
                    , example = "3", required = true, paramType = "query", dataType = "int")
    })
    @PostMapping("/ordinary/findRegionByCityId")
    public Result findRegionByCityId(Integer cityId) {
        List<Region> regionByCityId = regionService.findRegionByCityId(cityId);
        List<RegionDto> regionDtoList = CopyUtil.getList(regionByCityId, RegionDto.class);
        return regionDtoList == null ?
                new Result(false, StatusCode.ERROR, StatusMessageEnum.QUERY_IS_EMPTY.toString())
                : new Result(true, StatusCode.OK, StatusMessageEnum.SEARCH_SUCCESSFUL.toString(), regionDtoList);
    }


}

